import common.CommonDemo;
import org.asynchttpclient.util.Assertions;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.asserts.Assertion;

import java.time.Duration;

/**
 * use Java key enum
 * NULL         ('\uE000'),
 * CANCEL       ('\uE001'), // ^break
 * HELP         ('\uE002'),
 * BACK_SPACE   ('\uE003'),
 * TAB          ('\uE004'),
 * CLEAR        ('\uE005'),
 * RETURN       ('\uE006'),
 * ENTER        ('\uE007'),
 * SHIFT        ('\uE008'),
 * LEFT_SHIFT   (Keys.SHIFT),
 * CONTROL      ('\uE009'),
 * LEFT_CONTROL (Keys.CONTROL),
 * ALT          ('\uE00A'),
 * LEFT_ALT     (Keys.ALT),
 * PAUSE        ('\uE00B'),
 * ESCAPE       ('\uE00C'),
 * SPACE        ('\uE00D'),
 * PAGE_UP      ('\uE00E'),
 * PAGE_DOWN    ('\uE00F'),
 * END          ('\uE010'),
 * HOME         ('\uE011'),
 * LEFT         ('\uE012'),
 * ARROW_LEFT   (Keys.LEFT),
 * UP           ('\uE013'),
 * ARROW_UP     (Keys.UP),
 * RIGHT        ('\uE014'),
 * ARROW_RIGHT  (Keys.RIGHT),
 * DOWN         ('\uE015'),
 * ARROW_DOWN   (Keys.DOWN),
 * INSERT       ('\uE016'),
 * DELETE       ('\uE017'),
 * SEMICOLON    ('\uE018'),
 * EQUALS       ('\uE019'),
 * <p>
 * // Number pad keys
 * NUMPAD0      ('\uE01A'),
 * NUMPAD1      ('\uE01B'),
 * NUMPAD2      ('\uE01C'),
 * NUMPAD3      ('\uE01D'),
 * NUMPAD4      ('\uE01E'),
 * NUMPAD5      ('\uE01F'),
 * NUMPAD6      ('\uE020'),
 * NUMPAD7      ('\uE021'),
 * NUMPAD8      ('\uE022'),
 * NUMPAD9      ('\uE023'),
 * MULTIPLY     ('\uE024'),
 * ADD          ('\uE025'),
 * SEPARATOR    ('\uE026'),
 * SUBTRACT     ('\uE027'),
 * DECIMAL      ('\uE028'),
 * DIVIDE       ('\uE029'),
 * <p>
 * // Function keys
 * F1           ('\uE031'),
 * F2           ('\uE032'),
 * F3           ('\uE033'),
 * F4           ('\uE034'),
 * F5           ('\uE035'),
 * F6           ('\uE036'),
 * F7           ('\uE037'),
 * F8           ('\uE038'),
 * F9           ('\uE039'),
 * F10          ('\uE03A'),
 * F11          ('\uE03B'),
 * F12          ('\uE03C'),
 * <p>
 * META         ('\uE03D'),
 * COMMAND      (Keys.META),
 */
public class ActionKeyboard {

    private CommonDemo commonDemo;
    private ChromeDriver driver;
    private String url;

    @BeforeTest
    public void setUp() {
        try {
            commonDemo = new CommonDemo();
            driver = commonDemo.getChromeDriver();
            url = commonDemo.getUserDir("src/main/resources/actionBuilder.html");
            driver.get(url);
            driver.manage().window().maximize();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void keyboard() {
        new Actions(driver)
                .moveToElement(driver.findElement(By.id("action")))
                .clickAndHold()
                .sendKeys("1")
                .pause(Duration.ofSeconds(2))
                .keyDown(Keys.BACK_SPACE)
                .keyUp(Keys.BACK_SPACE)
                .perform();
    }

    /**
     * 模拟键盘输入大写字母
     */
    @Test
    public void capitalLetter() {
        new Actions(driver)
                .moveToElement(driver.findElement(By.id("action")))
                .clickAndHold()
                .keyDown(Keys.SHIFT)
                .sendKeys("abc")
                .keyUp(Keys.SHIFT)
                .perform();
    }

    /**
     * 模拟键盘复制粘贴
     */
    @Test
    public void copyAndPaste() {
        Keys cmdCtrl = Platform.getCurrent().is(Platform.MAC) ? Keys.COMMAND : Keys.CONTROL;
        WebElement element = driver.findElement(By.id("action"));
        new Actions(driver)
                .sendKeys(element, "演示")
                .pause(Duration.ofSeconds(2))//停顿，查看效果
                .keyDown(Keys.SHIFT)
                .sendKeys(Keys.ARROW_UP)
                .keyUp(Keys.SHIFT)
                .pause(Duration.ofSeconds(2))//停顿，查看效果
                .keyDown(cmdCtrl)
                .sendKeys("xvv")
                .keyUp(cmdCtrl)
                .perform();
        System.out.println(element.getAttribute("value"));
        Assert.assertEquals("演示演示", element.getAttribute("value"));

    }

    @AfterTest
    public void tearDown() {
        try {
            Thread.sleep(2000);
            driver.quit();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
